package com.ruoyi.expressList.service.lmpl;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.*;


import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.jy.common.index.Member;
import com.ruoyi.expressList.domain.ExpressAuth;
import com.ruoyi.expressList.domain.ExpressList;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.expressList.domain.ExpressDetial;
import com.ruoyi.expressList.domain.ExpressSystemMassage;
import com.ruoyi.expressList.mapper.ExpressListMapper;
import com.ruoyi.expressList.service.IExpressListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ExpressListServiceImpl implements IExpressListService
{
    @Autowired
    private ExpressListMapper expressListMapper;

    @Autowired
    private SnowflakeGenerator snowflakeGenerator;

    @Override
    public PageInfo<ExpressDetial> selectExpressListList(ExpressDetial expressList)
    {
        PageHelper.startPage(expressList.getPageNum(),expressList.getPageSize());
        List<ExpressDetial> expressListList = expressListMapper.selectExpressListList(expressList);
        PageInfo<ExpressDetial> expressListPageInfo = new PageInfo<>(expressListList);
        return expressListPageInfo;
    }


    @Override
    public PageInfo<ExpressDetial> selectExpressListListMember(ExpressDetial expressList) {
        PageHelper.startPage(expressList.getPageNum(),expressList.getPageSize());
        List<ExpressDetial> expressListList = expressListMapper.selectExpressListMemberList(expressList);
        return new PageInfo<>(expressListList);
    }

    @Override
    public PageInfo<ExpressAuth> authList(ExpressAuth expressAuth) {
        PageHelper.startPage(expressAuth.getPageNum(),expressAuth.getPageSize());
        List<ExpressAuth> expressAuthsList = expressListMapper.authList(expressAuth);
        return new PageInfo<>(expressAuthsList);
    }



    @Override
    public ExpressDetial getDetialList(Long id) {
        return expressListMapper.getDetialList(id);
    }


    @Override
    public ExpressDetial getDetialListPhone(String phone) {
        return  expressListMapper.getDetialListPhone(phone);
    }



    @Override
    public int updateStatusFalse(Long id) {
        return expressListMapper.updateStatusFalse(id);
    }

    @Override
    public int updateStatusTrue(Long id) {
        return expressListMapper.updateStatusTrue(id);
    }

    @Override
    public int resetPassword(Long id) {
        ExpressList expressList =expressListMapper.selectById(id);
        expressList.setPassword("123456");
        return  expressListMapper.resetPassword(expressList);

    }

    @Override
    public int sernullPayPassword(Long id) {
        ExpressList expressList =expressListMapper.selectById(id);
        expressList.setPayPassword(null);
//        修改支付密码状态
        expressList.setPayPasswordStatus(2);
        return  expressListMapper.sernullPayPassword(expressList);

    }

    @Override
    public int updateAccountStatusTrue(Long id) {
        return expressListMapper.updateAccountStatusTrue(id);
    }

    @Override
    public int updateAccountStatusFalse(Long id) {
        return expressListMapper.updateAccountStatusFalse(id);
    }

    @Override
    public int expressAdd(ExpressList expressList){
        expressList.setCreateDate(new Date());
        expressList.setIsDel(1);
        expressList.setIsVip(1);
        expressList.setRegisterType(2);
        expressList.setRegisterIp(snowflakeGenerator.next());
        ExpressDetial expressDetial = new ExpressDetial();
        expressDetial.setRegisterIp(expressList.getRegisterIp());
        expressDetial.setCreateDate(new Date());
        expressDetial.setStatus(1);
        expressListMapper.expressAddDetialRegisterIp(expressDetial);
        return expressListMapper.expressAdd(expressList);
    }



    @Override
    public int updateAuthFalse(Long id,Long iid,Integer authType) {
       ExpressAuth expressAuth = expressListMapper.selectAuthByid(id,iid);
       expressAuth.setStatusa(3L);
        return expressListMapper.updateAuthStatus(expressAuth);
    }


    @Override
    public int updateRefuseReason(ExpressAuth expressAuth) {


        if (expressAuth.getAuthType()==1){
            //快递员
            expressAuth.setPassDate(new Date());
            ExpressSystemMassage expressSystemMassage = new ExpressSystemMassage();
            expressSystemMassage.setRegisterIp(expressAuth.getRegisterIp());
            expressSystemMassage.setCreateDate(new Date());
            expressSystemMassage.setContent("拒绝了快递员认证审核");
            expressListMapper.insertSystemMessage(expressSystemMassage);

            ExpressList expressList= expressListMapper.getAuthById(expressAuth.getRegisterIp());
            expressList.setExpressAuthStatus(3);
            expressListMapper.updateAuthByid(expressList);

        }else{
            //实名认证
            expressAuth.setPassDate(new Date());
            ExpressSystemMassage expressSystemMassage = new ExpressSystemMassage();
            expressSystemMassage.setRegisterIp(expressAuth.getRegisterIp());
            expressSystemMassage.setCreateDate(new Date());
            expressSystemMassage.setContent("拒绝了实名认证审核");
            expressListMapper.insertSystemMessage(expressSystemMassage);

            ExpressList expressList= expressListMapper.getAuthById(expressAuth.getRegisterIp());
            expressList.setRealNameStatus(3);
            expressListMapper.updateRealNameAuthByid(expressList);
        }
        return expressListMapper.updateRefuseReason(expressAuth);
    }

    @Override
    public int updateAuthTure(Long iid,Long id,Integer authType) {
        ExpressAuth expressAuth = expressListMapper.selectAuthByid(id,iid);
        expressAuth.setStatusa(1L);
        expressAuth.setPassDate(new Date());

        if (authType==1){
            //快递员
            ExpressSystemMassage expressSystemMassage = new ExpressSystemMassage();
            expressSystemMassage.setRegisterIp(id);
            expressSystemMassage.setCreateDate(new Date());
            expressSystemMassage.setContent("同意了快递员认证审核");
            expressListMapper.insertSystemMessage(expressSystemMassage);

            ExpressList expressList= expressListMapper.getAuthById(id);
            expressList.setExpressAuthStatus(1);
            expressListMapper.updateAuthByid(expressList);

    }else{
            //实名认证
            ExpressSystemMassage expressSystemMassage = new ExpressSystemMassage();
            expressSystemMassage.setRegisterIp(id);
            expressSystemMassage.setCreateDate(new Date());
            expressSystemMassage.setContent("同意了实名认证审核");
            expressListMapper.insertSystemMessage(expressSystemMassage);

            ExpressList expressList= expressListMapper.getAuthById(id);
            expressList.setRealNameStatus(1);
            expressListMapper.updateRealNameAuthByid(expressList);
        }

        return expressListMapper.updateAuthStatus(expressAuth);
    }

    @Override
    public ExpressAuth getAuthDetialList(Long id,Integer iid) {
        return expressListMapper.getAuthDetialList(id,iid);
    }


    @Override
    public Member getMember() {
        ExecutorService executor = Executors.newFixedThreadPool(3); // 创建一个包含三个线程的线程池
        try {
            log.info("开始获取总数");

            Future<Integer> numFuture = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    return expressListMapper.getNum();
                }
            });

            Future<Integer> vipNumFuture = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    return expressListMapper.getVipNum();
                }
            });

            Future<Integer> courierNumFuture = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    return expressListMapper.getCourierNum();
                }
            });

            // 阻塞直到所有结果可用
            Integer num = numFuture.get();
            Integer vipNum = vipNumFuture.get();
            Integer courierNum = courierNumFuture.get();

            log.info("总数：{}, 会员：{}, 快递员：{}", num, vipNum, courierNum);
            return new Member(num, courierNum, vipNum);

        } catch (InterruptedException | ExecutionException e) {
            log.error("获取会员信息时发生错误", e);
            return null; // 异常时返回null或者其他处理
        } finally {
            executor.shutdown(); // 关闭线程池
        }
    }

    @Override
    public Member getNewAddNum() {

        ExecutorService executor = Executors.newFixedThreadPool(3); // 创建一个包含三个线程的线程池
        try {
            log.info("开始获取总数");

            Future<Integer> numFuture = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    return expressListMapper.getNewAddNum();
                }
            });

            Future<Integer> vipNumFuture = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    return expressListMapper.getNewAddVipNum();
                }
            });

            Future<Integer> courierNumFuture = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() {
                    return expressListMapper.getNewAddCourierNum();
                }
            });

            // 阻塞直到所有结果可用
            Integer num = numFuture.get();
            Integer vipNum = vipNumFuture.get();
            Integer courierNum = courierNumFuture.get();

            log.info("总数：{}, 会员：{}, 快递员：{}", num, vipNum, courierNum);
            return new Member(num, courierNum, vipNum);

        } catch (InterruptedException | ExecutionException e) {
            log.error("获取会员信息时发生错误", e);
            return null; // 异常时返回null或者其他处理
        } finally {
            executor.shutdown(); // 关闭线程池
        }
    }
}
